package com.coffee.framework.web.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.coffee.common.exception.CustomException;
import com.coffee.common.exception.DemoModeException;
import com.coffee.common.result.R;
import com.coffee.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.Objects;

/**
 * 全局异常处理器
 *
 * @author Kevin
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public R businessException(CustomException e) {
        if (Objects.isNull(e.getCode())) {
            return R.fail(e.getMessage());
        }
        return R.result(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public R handleNotLoginException(NotLoginException e) {
        log.error(e.getMessage(), e);
        return R.result(ResultCode.UN_AUTHORIZED);
    }

    @ExceptionHandler(NotRoleException.class)
    public R handleNotRoleException(NotRoleException e) {
        log.error(e.getMessage(), e);
        return R.result(ResultCode.UN_AUTHORIZED);
    }

    @ExceptionHandler(NotPermissionException.class)
    public R handleNotPermissionException(NotPermissionException e) {
        log.error(e.getMessage(), e);
        return R.result(ResultCode.UN_AUTHORIZED);
    }

    @ExceptionHandler(RuntimeException.class)
    public R handleRuntimeException(RuntimeException e) {
        log.error(e.getMessage(), e);
        return R.result(ResultCode.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        log.error(e.getMessage(), e);
        return R.result(ResultCode.INTERNAL_SERVER_ERROR);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public R validatedBindException(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return R.fail(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public R constraintViolationException(ConstraintViolationException e) {
        log.error(e.getMessage(), e);
        String message = e.getConstraintViolations().iterator().next().getMessage();
        return R.fail(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validExceptionHandler(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return R.fail(message);
    }

    /**
     * 演示模式异常
     */
    @ExceptionHandler(DemoModeException.class)
    public R demoModeException(DemoModeException e) {
        return R.fail("演示模式，不允许操作");
    }
}
